package service;
import java.math.BigDecimal;
import java.util.List;

import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import common.kit.AmountKit;
import common.kit.PageSql;
import enums.CompareCheckType;
import model.CompareCheck;
import model.CompareCheckGroup;
import model.Staff;
import model.Station;

public class CompareCheckService extends BaseService {

	public Page<Record> getPage(Kv kv , Integer pageNumber , Integer pageSize) {
		String select = " tcc.id,tcc.createTime,tcc.type,tcc.name,tcc.unit,tcc.compareData,tcc.onlineData,tcc.error,tcc.standard,tcc.result,tcc.testing,tcc.testingId,ts.name AS stationName,tc.name AS companyName1,tsr.name AS servicerName2,tcc.recorder " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_compare_check tcc ") ;
	    pageSql.join(" t_station ts " , " ts.id = tcc.stationId ") ;
	    pageSql.join(" t_company tc " , " tc.id = tcc.companyId ") ;
	    pageSql.join(" t_servicer tsr " , " tsr.id = tcc.serverId ") ;
		pageSql.setNotLessThanCondition("tcc.createTime","tcc.createTime.start") ; //创建时间
		pageSql.setNotMoreThanCondition("tcc.createTime","tcc.createTime.end") ; //创建时间
		pageSql.setEqualCondition("tcc.type") ; //类型	 
		pageSql.setLikeCondition("tcc.name") ; //项目	 
		pageSql.setLikeCondition("tcc.testingId") ; //检测报告号	 
		pageSql.setEqualCondition("tcc.stationId") ; //点位	 
	    pageSql.orderBy(" tcc.createTime DESC ");
		Page<Record> page = pageSql.paginate( pageNumber , pageSize) ;
		for (Record record : page.getList()) {
			record.set("typeStr", CompareCheckType.getTitleByValue(record , "type"));
		}
	    return page ;
	}

	public Page<Record> getApiPage(Kv kv , Integer pageNumber , Integer pageSize) {
		String select = " tcc.id,tcc.createTime,tcc.testing,tcc.testingId,tcc.recorder,ts.name AS servicerName " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_compare_check_group tcc ") ;
	    pageSql.join(" t_servicer ts " , " ts.id = tcc.serverId ") ;
		pageSql.setEqualCondition("tcc.stationId") ; //点位
		pageSql.setNotLessThanCondition("tcc.createTime", "startTime");
		pageSql.setNotMoreThanCondition("tcc.createTime", "endTime");
	    pageSql.orderBy(" tcc.createTime DESC ");
		Page<Record> page = pageSql.paginate( pageNumber , pageSize) ;
		List<Record> list = page.getList();
		for (Record record : list) {
			Long groupId = record.getLong("id");
			List<CompareCheck> items = CompareCheck.dao.findByGroupId(groupId);
			record.set("list", items);
		}
		return page ;
	}
	
	public Ret save(List<CompareCheck> list , CompareCheckGroup checkGroup , Staff staff , Station station) {
		checkGroup.setCompanyId(station.getCompanyId());
		checkGroup.setServerId(staff.getServicerId());
		checkGroup.setRecorder(staff.getName());
		checkGroup.setStationId(station.getId());
		checkGroup.save() ;
		
		StringBuffer names = new StringBuffer();
		StringBuffer results = new StringBuffer();
		for (CompareCheck check : list) {
			if (check.getError().compareTo(BigDecimal.ZERO)== -1 || check.getError().compareTo(AmountKit.ONE_HUNDRED) == 1) {
				throw new RuntimeException("误差值有误");
			}
			check.setGroupId(checkGroup.getId());
//			check.setType(station.getType());
			check.setCompanyId(station.getCompanyId());
			check.setServerId(staff.getServicerId());
			check.setRecorder(staff.getName());
			check.save();
			names.append(names.length() == 0 ? "" : "、" ).append(check.getName());
			results.append(results.length() == 0 ? "" : "、" ).append(check.getName()).append(check.getResult());
		}
		
		//保存比对维护记录
		new MaintainRecordsService().saveCompare( names.toString() , results.toString() , station, staff);
		
		return ok() ;
	}
}
